<?php
namespace  App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Repair;
use App\Models\ServiceType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RepairController extends Controller
{
    /*
     * 保修首页列表
     */
    public function index(Request $request,Repair $repair,ServiceType $serviceType)
    {
        if(isset($request->condition) && !empty($request->condition)){
            $res = $repair->indexPageLike($request->page,$request->limit,$request->condition);
        }else{
            $res = $repair->indexPage($request->page,$request->limit);
        }
        foreach($res as $k=>$v){
            $service = $serviceType->where('id',$v['type'])->get();
            $res[$k]['type'] = json_decode($service,true)[0]['name'];
        }
        if($res && count($res) >0){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','查询失败');
        }
    }
    /*
    * 报修记录数
    */
    public function indexNum(Request $request,Repair $repair)
    {
        if(isset($request->condition) && !empty($request->condition)){
            $condition = $request->condition;
//            $res = $repair->where('order_id','like',"%$request->condition%")->where('status',1)->count();
            $query = 'SELECT COUNT(*) as count FROM `repair` WHERE `status` = 1 and CONCAT(`order_id`,`desc`,`username`,`address`,`service_at`,`type`) LIKE ?';
            $param = '%'.$condition.'%';
            $count = DB::select($query,array($param));
            $res = $count[0]->count;
        }else{
            $res = $repair->where('status',1)->count();
        }
        if($res){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','查询失败');
        }
    }
    /*
     * 添加报修记录
     */
    public function save(Request $request,Repair $repair)
    {
        //封装验证规则
        $param = $request->all();
        $this->validate($request, [
            'username' => 'required',
            'number' => 'required|integer'
        ],[
            'username.required' => '姓名不能为空',
            'number.required' => '服务数量不能为空',
            'number.integer' => '服务数量必须为数字'
        ]);
        //生成订单号
        $param['order_id'] = time().rand(1000,9999);
        //记录状态默认1
        $param['status'] = 1;
        $code = $repair->create($param);
        if($code){
            return $this->return_json(200,$code,'添加成功');
        }else{
            return $this->return_json(201,'','添加失败');
        }
    }
    /*
     * 修改保修记录
     */
    public function update(Request $request,Repair $repair)
    {
        $param = $request->all();
        $param['status'] = 1;
        $code = $repair->whereid($request->id)->update($param);
        if($code){
            return $this->return_json(200,'','修改成功');
        }else{
            return $this->return_json(201,'','修改失败');
        }
    }
    /*
     * 渲染修改页面
     * 根据报修记录id获取记录详情
     */
    public function getRepairById(Request $request,Repair $repair)
    {
        $res = $repair->whereid($request->id)->get();
        if($res && !$res->isEmpty()){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','查询失败');
        }
    }
    public function delete(Request $request,Repair $repair)
    {
//        $this->validate($request,
//            ['id' => 'required'],
//            ['id.required' => 'id不能为空']);
        $param = $request->all();
        $param['status'] = 0;
        $code = $repair->whereid($request->id)->update($param);
        if($code){
            return $this->return_json(200,'','删除成功');
        }else{
            return $this->return_json(201,'','删除失败');
        }
    }

}
